package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseCollect;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.CourseCollectVo;
import com.atguigu.guli.service.edu.mapper.CourseCollectMapper;
import com.atguigu.guli.service.edu.service.CourseCollectService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author liujun
 * @since 2021-01-20
 */
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {

    @Autowired
    private CourseCollectService courseCollectService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    @Override
    public List<CourseCollectVo> selectListByMemberId(String memberId) {
        //1、收藏信息
        final List<CourseCollect> courseCollects = courseCollectService.list(new QueryWrapper<CourseCollect>().eq("member_id", memberId));
        if (!CollectionUtils.isEmpty(courseCollects)) {
            final List<CourseCollectVo> collectVos = courseCollects.stream().map(courseCollect -> {
                final CourseCollectVo courseCollectVo = new CourseCollectVo(courseCollect.getId(), courseCollect.getCourseId(), courseCollect.getGmtCreate());
                //2、课程信息
                final Course course = courseService.getById(courseCollect.getCourseId());
                if (course != null) {
                    courseCollectVo.setCover(course.getCover());
                    courseCollectVo.setLessonNum(course.getLessonNum());
                    courseCollectVo.setTitle(course.getTitle());
                    courseCollectVo.setPrice(course.getPrice());
                    //3、老师信息
                    final Teacher teacher = teacherService.getById(course.getTeacherId());
                    if (teacher != null) {
                        courseCollectVo.setTeacherName(teacher.getName());
                    }
                }
                return courseCollectVo;
            }).collect(Collectors.toList());
            return collectVos;
        }
        return new ArrayList<CourseCollectVo>();
    }
}
